﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    /// <summary>
    /// 自定义磅单
    /// </summary>
    public class SysCustomizeBillDao : BaseDao<CustomizeBill>
    {
        #region instance

        private static SysCustomizeBillDao? instance;
        public static SysCustomizeBillDao Instance => instance ??= new SysCustomizeBillDao();

        public override string GetTableName()
        {
            return "sys_customize_bill";
        }
        #endregion


        public int Insert(CustomizeBill customizeBill)
        {
            string sql = SqlBuilder.GetInsertSql(customizeBill, GetTableName());
            return Sqlite.Insert(sql);
        }


        public  int Update(CustomizeBill customizeBill)
        {
            string sql = SqlBuilder.GetUpdateSql(customizeBill, GetTableName());
            return Sqlite.Update(sql);
        }

        public int Delete(CustomizeBill customizeBill)
        {
            string sql = SqlBuilder.GetDeleteSql(GetTableName(),$"{SysCustomizeBillField.id} = {customizeBill.id}");
            return Sqlite.Delete(sql);
        }
        public CustomizeBill? FindByNameAndType(string name, WeightingBillType type)
        {
            string condition = $"{SysCustomizeBillField.company_id} = {App.CurrentCompanyVo.Id} and {SysCustomizeBillField.name} = '{name}' and {SysCustomizeBillField.type} = {(int)type}";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition);
            var data = Sqlite.Select<CustomizeBill>(sql);
            return data.FirstOrDefault();
        }

        public List<CustomizeBill> SelectByBillType(WeightingBillType type)
        {
            string condition = $"({SysCustomizeBillField.company_id} = {App.CurrentCompanyVo.Id} OR {SysCustomizeBillField.company_id} = 0 )  and {SysCustomizeBillField.type} = {(int)type}";
            String order = $"{SysCustomizeBillField.add_time} desc";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition,null,null,order);
            var data = Sqlite.Select<CustomizeBill>(sql);
            return data;
        }

        public CustomizeBill? FindUserdByType(WeightingBillType type)
        {
            string condition = $"({SysCustomizeBillField.company_id} = {App.CurrentCompanyVo.Id} OR {SysCustomizeBillField.company_id } = 0 ) and {SysCustomizeBillField.type} = {(int)type} and {SysCustomizeBillField.is_used} = 1";
            String order = $"{SysCustomizeBillField.add_time} desc";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, order);
            var datas = Sqlite.Select<CustomizeBill>(sql);
            return datas.FirstOrDefault();
        }

        public int UseingCustomer(CustomizeBill cb)
        {
            if (cb.type == null) return 0;
            var ucb = FindUserdByType((WeightingBillType)cb.type);
            if(ucb != null)
            {
                ucb.isUsed = false;
                Update(ucb);
            }
            cb.isUsed = true;
            return Update(cb);
        }

        [Obsolete("方法多余", true)]
        public List<CustomizeBill> SelectExitSlips()
        {
            string condition = $"{SysCustomizeBillField.company_id} = {App.CurrentCompanyVo.Id} and {SysCustomizeBillField.type} = {(int)WeightingBillType.ExitSlip}";
            String order = $"{SysCustomizeBillField.add_time} desc";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, order);
            var data = Sqlite.Select<CustomizeBill>(sql);
            return data;
        }
    }
}
